package com.adk.backend.controller;

import com.adk.backend.annotation.RequiresPermission;
import com.adk.backend.common.Result;
import com.adk.backend.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表统计控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/report")
public class ReportController {
    
    @Autowired
    private ReportService reportService;
    
    /**
     * 获取订单统计（数据看板）
     */
    @RequiresPermission("view")
    @GetMapping("/order")
    public Result<Map<String, Object>> getOrderStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Long uid) {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("uid", uid);
        
        Map<String, Object> result = reportService.getOrderStatistics(params);
        return Result.success(result);
    }
    
    /**
     * 获取财务统计
     */
    @RequiresPermission("view")
    @GetMapping("/finance")
    public Result<Map<String, Object>> getFinanceStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        
        Map<String, Object> result = reportService.getFinanceStatistics(params);
        return Result.success(result);
    }
    
    /**
     * 获取仓库统计
     */
    @RequiresPermission("view")
    @GetMapping("/warehouse")
    public Result<Map<String, Object>> getWarehouseStatistics() {
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> result = reportService.getWarehouseStatistics(params);
        return Result.success(result);
    }
    
    /**
     * 按业务员统计订单（report.php的orders功能）
     */
    @RequiresPermission("view")
    @GetMapping("/orders-by-sales")
    public Result<List<Map<String, Object>>> getOrdersReportBySales(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Long uid) {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("uid", uid);
        
        List<Map<String, Object>> result = reportService.getOrdersReportBySales(params);
        return Result.success(result);
    }
    
    /**
     * 贸易订单提单号列表（report.php的gt功能）
     */
    @RequiresPermission("view")
    @GetMapping("/trade-order-awb")
    public Result<List<Map<String, Object>>> getTradeOrderAwbList(
            @RequestParam(required = false) Integer shipType,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        Map<String, Object> params = new HashMap<>();
        params.put("shipType", shipType);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        
        List<Map<String, Object>> result = reportService.getTradeOrderAwbList(params);
        return Result.success(result);
    }
    
    /**
     * 贸易订单统计（report.php的gts功能）
     */
    @RequiresPermission("view")
    @GetMapping("/trade-order-statistics")
    public Result<Map<String, Object>> getTradeOrderStatistics(
            @RequestParam(required = false) Integer shipType,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        Map<String, Object> params = new HashMap<>();
        params.put("shipType", shipType);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        
        Map<String, Object> result = reportService.getTradeOrderStatistics(params);
        // 如果返回的code不为1，说明有错误
        if (result.get("code") != null && !result.get("code").equals(1)) {
            return Result.error(result.get("msg") != null ? result.get("msg").toString() : "统计失败");
        }
        return Result.success(result);
    }
    
    /**
     * 获取贸易运输方式列表
     */
    @RequiresPermission("view")
    @GetMapping("/trade-ship-types")
    public Result<List<Map<String, Object>>> getTradeShipTypes() {
        List<Map<String, Object>> result = reportService.getTradeShipTypes();
        return Result.success(result);
    }
}


